package com.ny.controller;

import com.ny.entity.Result;
import com.ny.entity.User;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ny.entity.Device;
import com.ny.service.DeviceService;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private DeviceService deviceService;
    // json测试：http://127.0.0.1:8081/register  POST
    // @RequestBody的意思：表示将请求体中的JSON数据映射为User对象
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
       int i = userService.addUser( user);
        if (i>0) {
            return Result.success("注册成功");
        }
        return Result.error("注册失败");
    }

    //登录测试：http://127.0.0.1:8081/login
    @GetMapping("/login")
    public Result login(String username, String password) {
        User user = userService.findUser(username, password);
        if (user != null) {
            return Result.success(user);
        }
        else {
            return Result.error("用户名或密码错误");
        }
    }

    @GetMapping("/findUserByEmail")
    public Result findUserByEmail(String email){
        User user =  userService.findUserByEmail( email);
        if ( user!=null){
            return Result.success(user);
        }
        else {
            return Result.error("该QQ邮箱号不存在");
        }
    }

    @GetMapping("/sendEmail")
    public Result sendEmail(String toEmail){
        if (toEmail!=null&& userService.findUserByEmail(toEmail)!=null){
            userService.sendToEmail(toEmail);
            return  Result.success("发送成功");
        }
        return Result.error("发送失败,该QQ邮箱号不存在");

    }

    // 修改密码：http://127.0.0.1:8081/updatePassword?code=123456&password=123456&email=<EMAIL>
    @PutMapping("/updatePassword")
    public Result updatePassword(String code,String password,String email){
        // 判断验证码是否正确，如果正确，就修改密码
        if (userService.checkCode(email,code)){
            int i = userService.updatePassword(password,email);
            return Result.success("修改密码成功");
        }
        return Result.error("修改密码失败");
    }


        /**
         * 查询所有设备信息
         * @return 设备列表结果
         */
        @GetMapping("/devices")
        public Result getAllDevices() {
            try {
                List<Device> devices = deviceService.getAllDevices();
                return Result.success(devices);
            } catch (Exception e) {
                return Result.error("查询设备信息失败");
            }
        }

    /**
     * 根据设备名称查询设备信息
     * @param deviceName 设备名称
     * @return 设备列表结果
     */
    @GetMapping("/devices/search")
    public Result getDevicesByName(@RequestParam String deviceName) {
        try {
            List<Device> devices = deviceService.getDevicesByName(deviceName);
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("查询设备信息失败");
        }
    }
}

